using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using DIS_DAL_WS.Models;

namespace DIS_DAL_WS.DTO
{
    [DataContract]
	public class OrderDetailDTO
	{
        [DataMember]
		public int ID { get; set; }
        [DataMember]
		public int IDOrder { get; set; }
        [DataMember]
		public int IDItem { get; set; }
        [DataMember]
		public int Quantity { get; set; }
        [DataMember]
		public double Price { get; set; }
        [DataMember]
		public double Discount { get; set; }
        [DataMember]
        public ItemDTO Item { get; set; }
        [DataMember]
        public OrdersDTO Order { get; set; }

        public static OrderDetailDTO Translate(OrderDetail Entity)
        {
            if (Entity != null)
            {
                var dto = new OrderDetailDTO
                {
                    ID = Entity.ID,
                    Quantity = Entity.Quantity == null ? 0 : Entity.Quantity.Value,
                    Price = Entity.Price == null ? 0 : Entity.Price.Value,
                    Discount = Entity.Discount == null ? 0 : Entity.Discount.Value,
                    Item = ItemDTO.Translate(Entity.Item),
                    Order = OrdersDTO.TranslateBase(Entity.Orders)
                };
                if (Entity.IDOrder != null)
                {
                    dto.IDOrder = Entity.IDOrder.Value;
                }
                else
                {
                    dto.IDOrder = -1;
                }
                if (Entity.IDItem != null)
                {
                    dto.IDItem = Entity.IDItem.Value;
                }
                else
                {
                    dto.IDItem = -1;
                }
                return dto;
            }
            return null;
        }

        public static OrderDetail Reverse(OrderDetailDTO obj)
        {
            var entity = new OrderDetail
            {
                ID = obj.ID,
                Quantity =obj.Quantity,
                Price = obj.Price,
                Discount = obj.Discount
            };
            if (obj.IDOrder != -1)
            {
                entity.IDOrder = obj.IDOrder;
            }
            if (obj.IDItem != -1)
            {
                entity.IDItem = obj.IDItem;
            }
            return entity;
        }

        public void UpdateEntity(ref OrderDetail entity)
        {
            entity.Discount = Discount;
            entity.Quantity = Quantity;
            entity.Price = Price;
            if (IDOrder != -1)
            {
                entity.IDOrder = IDOrder;
            }
            if (IDItem != -1)
            {
                entity.IDItem = IDItem;
            }
        }
	}
}